Skip to content

[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector#372

Merged
TomasVotruba merged 1 commit into
rectorphp:mainfrom
jquiaios:add-return-type-will-change-attribute-rector
May 30, 2026
Merged

[DowngradePhp81] Add AddReturnTypeWillChangeAttributeRector#372
TomasVotruba merged 1 commit into
rectorphp:mainfrom
jquiaios:add-return-type-will-change-attribute-rector

Conversation

@jquiaios
Copy link
Copy Markdown
Contributor

@jquiaios jquiaios commented May 8, 2026

Hello there 👋

Here's a proposition to fix this old issue rectorphp/rector#9515

Here's a global explanation of the issue and proposed fix (if necessary):

PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate and Stringable), triggering deprecation notices on implementations without matching return types.

I created a new AddReturnTypeWillChangeAttributeRector rule that automatically detects affected methods, and adds #[\ReturnTypeWillChange] to suppress those notices.

The rule is registered in the downgrade-php81 set so it runs automatically for anyone using ->withDowngradeSets(php81: true).

The rule is covered by unit tests, including fixtures for the positive cases (ArrayAccess, Countable, Iterator, IteratorAggregate and Stringable), and skip cases (method already has a return type, attribute already present).

- PHP 8.1 added provisional return types to built-in interfaces (ArrayAccess, Countable, Iterator, IteratorAggregate, Stringable), triggering deprecation notices on implementations without matching return types.
- This rule automatically adds #[\ReturnTypeWillChange] to affected methods, suppressing those notices for codebases that need to remain compatible with older PHP versions and cannot yet add the required return types.

Fixes: rectorphp/rector#9515
@TomasVotruba TomasVotruba requested a review from samsonasik May 26, 2026 08:14
@TomasVotruba
Copy link
Copy Markdown
Member

Hi @jquiaios , thanks for the fix 👍 Let's ship it and see how it performs on Rector codebase downgrade

@TomasVotruba TomasVotruba merged commit cbb17de into rectorphp:main May 30, 2026
8 checks passed
{
foreach ($classMethod->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->getLast() === self::RETURN_TYPE_WILL_CHANGE) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getLast() is invalid here, it can be a namespaced class, and it will only pick last end of explode \\ string.

Should just use existing rector service Rector\Php80\NodeAnalyzer\PhpAttributeAnalyzer::hasPhpAttribute()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants